package org.jeecg.modules.demo.st.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.st.entity.StStateJilu;
import org.jeecg.modules.demo.st.entity.StStudent;
import org.jeecg.modules.demo.st.mapper.StProfessionMapper;
import org.jeecg.modules.demo.st.mapper.StStateJiluMapper;
import org.jeecg.modules.demo.st.service.IStStateJiluService;
import org.jeecg.modules.demo.st.service.IStStudentService;
import org.jeecg.modules.demo.st.vo.StStateJiluForAdmin;
import org.jeecg.modules.demo.st.vo.StatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: 学生状态记录
 * @Author: jeecg-boot
 * @Date:   2019-10-26
 * @Version: V1.0
 */
@Service
public class StStateJiluServiceImpl extends ServiceImpl<StStateJiluMapper, StStateJilu> implements IStStateJiluService {
    @Autowired
    private IStStudentService stStudentService;
    @Autowired
    private StStateJiluMapper stStateJiluMapper;


    /**
     * 学生状态记录
     * 确认归校
     *comeback
     */
   @Override
    public  boolean comeback(StStateJilu stStateJilu) {
         //创建一个学生实体
        StStudent stStudent=new StStudent();
        //学生记录拿到学生姓名存放到学生信息中
        stStudent.setId(stStateJilu.getStName());
        //配置字典中  1表示在校
        stStudent.setStatus("1");
       LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
       stStateJilu.setConfirmor(sysUser.getId());
       stStateJilu.setConfirmTime(new Date());
       boolean ok =false;
       QueryWrapper<StStateJilu> stStateJiluQueryWrapper=new QueryWrapper<>();
       stStateJiluQueryWrapper.eq("st_name",stStateJilu.getStName());
       stStateJiluQueryWrapper.orderByDesc("create_time");
       IPage page=new Page();
       page.setPages(1);
       page.setSize(1);
       IPage<StStateJilu> page1 = this.page(page, stStateJiluQueryWrapper);
       if(page1.getRecords().size()>0){
           stStateJilu.setId(page1.getRecords().get(0).getId());
               if(this.updateById(stStateJilu)){
                   ok= stStudentService.updateById(stStudent);
               }
           }
        return ok;
    }

    @Override
    public boolean updateStatus(StStateJiluForAdmin stStateJiluForAdmin) {
        return false;
    }

    @Override
    public List<StatisticsVo> getFrequency(String stuId){
        List<StatisticsVo> getFrequency= stStateJiluMapper.getFrequency(stuId);
        return getFrequency;
    }

    @Override
    public List<StStateJilu> getSchoolRecording(String stName) {
        List<StStateJilu> getFrequency= stStateJiluMapper.getSchoolRecording(stName);
        return getFrequency;
    }

    @Override
    public List<StStateJilu> getRecording(String stuId) {
        List<StStateJilu> getRecording= stStateJiluMapper.getRecording(stuId);
        return getRecording;
    }

    @Override
    public  List<StStateJilu> getLast(String stuId){
         return  stStateJiluMapper.getLast(stuId);
    }

    @Override
    public List<StStateJilu> getLasts(List<String> stuId) {
        String join = String.join(",", stuId);
        return  stStateJiluMapper.getLasts(join);
    }
}
